package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.PubStoryAdapter;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.StoryPicCard;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.init.RecycleOper;
import reduce.fz.com.reduce.photo.PhotoWallActivity;
import reduce.fz.com.reduce.service.StoryService;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.photo.PhotoSelect;
import reduce.fz.com.reduce.util.photo.SelectParam;

public class PubShareStoryActivity extends BaseToolBarActivity implements View.OnClickListener {

    //发布经验故事列表
    public RecyclerView mStoryListView;
    //故事卡片
    private List<StoryPicCard> picCards = null;
    //故事服务
    private StoryService storyService = ServiceBuilder.builderStoryService(this);
    //发布故事
    private PubStoryAdapter mAdapter;

    private TextView tvSave;

    private static final int EDITCONTENT = 121;

    private List<String> seletPaths = null;
    private PhotoSelect photoSelect;

    private int editPosition;
    private StoryPicCard editcard;

    public void initPhotoSelect() {
        SelectParam param = new SelectParam();
        photoSelect = PhotoSelect.instance(this, param);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pub_share_story);
        initWidget();
        initRecycleViewConfig();
        initBtnLocation();
        initdata();
    }

    public void initdata() {
        seletPaths = getIntent().getStringArrayListExtra(PhotoWallActivity.IMAGE_SELECT_RESULT);
        mAdapter.init(seletPaths);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PhotoSelect.SELECT_PIC:
                ArrayList<String> paths = photoSelect.getSelectPaths(data);
                editcard.localpth = paths.get(0);
                mAdapter.notifyContentItemChanged(editPosition);
                break;
            case EDITCONTENT:
                String content = data.getStringExtra("pictext");
                editcard.desc = content;
                mAdapter.notifyContentItemChanged(editPosition);
                break;
            default:
                break;
        }
    }

    /**
     * 初始化保存按钮的样式和边距
     */
    public void initBtnLocation() {
        TextView tvSave = (TextView) getLayoutInflater().inflate(R.layout.textview_save_toolbar, mToolbar, true);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int padding = getResources().getDimensionPixelSize(R.dimen.dimen_30);
        tvSave.setPadding(padding, padding, padding, padding);
    }

    public void pubStory() {
        final LoadingDialog dialog = new LoadingDialog(this);
        dialog.setTitle("提交中,请稍候");
        dialog.show();
        StoryCard card = mAdapter.getStoryCard();
        storyService.pubStoryCard(card, new AbResponseListener(new TypeReference<Result>() {
        }) {
            @Override
            public void responseInfo(Result data) {
                dialog.dismiss();
                finish();
            }
        });

    }

    public void initRecycleViewConfig() {
        mAdapter = new PubStoryAdapter(this, new PubStoryAdapter.OnPicItemListener() {
            @Override
            public void onSelectImage(int position, StoryPicCard card) {

                selectPic(position, card);

            }

            @Override
            public void onEditComment(int position, StoryPicCard card) {
                forwardEdit(position, card);
            }
        });

        mStoryListView.setAdapter(mAdapter);
        RecycleOper oper = new RecycleOper(this, mStoryListView);
        oper.setDefault();
    }

    public void selectPic(int position, StoryPicCard card) {
        editcard = card;
        editPosition = position;
        photoSelect.selectPhoto();
    }

    public void forwardEdit(int position, StoryPicCard card) {
        editcard = card;
        editPosition = position;
        Intent intent = new Intent(this, StoryPicTextEditActivity.class);
        startActivityForResult(intent, EDITCONTENT);
    }


    @Override
    public void initWidget() {
        mStoryListView = (RecyclerView) findViewById(R.id.story_list_view);
    }

    @Override
    public void initListener() {
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_pub_story:
                pubStory();
                break;
            default:
                break;
        }

    }


}
